package com.zuoyeben123.testzybhtml;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


/**
 * Created by wangliang on 2017/3/7.
 */

public class CommonWebView extends WebView {
    private static final boolean DEBUG_WEB = false;

    private boolean mIsTouch = true;
    private OnPageFinishCallback mOnPageFinished;


    public CommonWebView(Context context) {
        super(context);
        init();
    }

    public CommonWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CommonWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        WebSettings localWebSettings = getSettings();
        localWebSettings.setBuiltInZoomControls(false);
        localWebSettings.setSupportZoom(false);
        localWebSettings.setAllowFileAccess(true);
        localWebSettings.setDisplayZoomControls(false);
        localWebSettings.setLoadWithOverviewMode(true);
        localWebSettings.setUseWideViewPort(true);// 如果为true，整体就会随宽度变化而变化
        localWebSettings.setDomStorageEnabled(true);
        localWebSettings.setJavaScriptEnabled(true);

        localWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        localWebSettings.setAppCacheEnabled(false);


        if (Build.VERSION.SDK_INT >= 16) {
            localWebSettings.setAllowUniversalAccessFromFileURLs(true);
        }

        if(DEBUG_WEB) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                WebView.setWebContentsDebuggingEnabled(true);
            }
        }

        setWebChromeClient(new WebChromeClient());

        setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(final WebView view, String url) {
                super.onPageFinished(view, url);
                view.loadUrl("javascript:window.bridge.getSource(document.getElementsByTagName('pre')[0].innerHTML);");
                if(mOnPageFinished != null) {
                    mOnPageFinished.onPageFinished(view, url);
                }
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if(mOnPageFinished != null) {
                    mOnPageFinished.onReceiveError(errorCode,description);
                }
            }
        });

        setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View paramAnonymousView) {
                return true;
            }
        });

        setBackgroundColor(0);
    }

    public void loadByHtmlContent(String html) {
        loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
    }

    public void setTouchable(boolean enable) {
        mIsTouch = enable;
    }

    public void setOnPageFinishCallback(OnPageFinishCallback callback) {
        this.mOnPageFinished = callback;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(mIsTouch) {
            return super.onTouchEvent(ev);
        } else {
            return false;
        }
    }

    public interface OnPageFinishCallback {
        void onPageFinished(WebView view, String url);
        void onReceiveError(int errorCode, String desc);
    }

}
